package com.hdyg.sixg.util;

import android.content.Context;
import android.os.Looper;
import android.support.annotation.StringRes;
import android.widget.Toast;

import com.hdyg.sixg.app.MvpApplication;


/**
 * Toast
 *
 * @author CZB
 * @time 2018/7/5 18:06
 */
public class ToastUtil {
    private static Context context = MvpApplication.getContext();
    private static Toast toast;

    public static void show(@StringRes int resId) {
        show(context.getResources().getString(resId));
    }

    // Toast.LENGTH_SHORT 2秒，Toast.LENGTH_LONG 3秒
    public static void show(CharSequence text) {
        try {
            if (toast != null) {
                toast.setText(text);
            } else {
                toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            }
            toast.show();
        } catch (Exception e) {
            // 解决在子线程中调用Toast的异常情况处理
            Looper.prepare();
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            toast.show();
            Looper.loop();
        }
    }
}
